﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using SeAssignment.Models;
using Common;
using BusinessLayer;

namespace SeAssignment.Controllers
{
    public class AccountController : Controller
    {
        public ActionResult Login()
        {
            return View();
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("HomePage", "Home");
        }

        [HttpPost]
        public ActionResult Login(Models.UserLogin user)
        {

            if (new UsersBL().UserLogin(user.UserName, user.Password) == true)
            {
                FormsAuthentication.SetAuthCookie(user.UserName, user.RememberMe);
                return RedirectToAction("HomePage", "Home");
            }
            else
            {
                ModelState.AddModelError("", "Login data is incorrect you dummy!!!");
            }

            return View("Login");

        }

        public JsonResult DoesUsernameExists(string UserName)
        {
            bool doesExist = new UsersBL().CheckUsernameExists(UserName);

            if (!doesExist)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }

            return Json(false, JsonRequestBehavior.AllowGet);
        }

        public JsonResult DoesEmailExists(string Email)
        {

            bool doesExist = new UsersBL().CheckEmailExists(Email);

            if (!doesExist)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }

            return Json(false, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public bool isUserLoggedin()
        {
            if (HttpContext.User.Identity.Name != "")
                return true;
            else
                return false;
        }

        public ActionResult Registration()
        {
            return View(new RegisterModel());
        }

        [HttpPost]
        public ActionResult Registration(SeAssignment.Models.RegisterModel r)
        {
            User user = new User();
            user.UserName = r.Username;
            user.Password = r.Password;
            user.Email = r.Email;
            user.Title = r.Title;
            user.Name = r.Name;
            user.Surname = r.Surname;
            user.DOB = r.DOB;
            user.Telephone = r.Telephone;
            user.Gender = r.Gender.DataTextField;
            user.HouseNo = r.HouseNo.ToString();
            user.StreetName = r.StreetName;
            user.TownID = Convert.ToInt32(r.TownList.DataValueField);
            user.RoleID = Convert.ToInt32(r.AccountType.DataValueField);
            user.MobileNumber = r.MobileNo;
            new UsersBL().AddUser(user);
            return RedirectToAction("HomePage", "Home");
        }
    }

}
